mui.init();
var member_id = ws = spage = order_id = flea = '';
document.addEventListener('plusready',function(){
	member_id = plus.storage.getItem('id');
	ws = plus.webview.currentWebview();
	spage = ws.spage;
	order_id = ws.order_id;
	evaluation_state = ws.evaluation_state;
//	console.log(member_id);
	$.ajax({
		type:"get",
		url:apiRoot + "?act=member_order&op=order_info",
		data:{
			member_id:member_id,
			order_id:order_id
		}, 
		dataType:'json',
		success:function(data){
			console.log(JSON.stringify(data));
			if(data.code == 200){
				var html = list = '';
				$('.order_sn').text(data.datas.order_info.order_sn);
				$('.shouhu span').eq(0).text("收货人："+data.datas.order_info.reciver_name);
				$('.shouhu span').eq(1).text(data.datas.order_info.reciver_phone);
				$('.reciver_addr').text(data.datas.order_info.reciver_addr);
				$('.order_message').text(data.datas.order_info.order_message);
				$('.xiangq span map').text(data.datas.order_info.order_amount);
				if(data.datas.order_info){
								switch(data.datas.order_info.state_desc){
									case '已取消':
										html += '<button type="button" class="mui-btn">已取消</button>';
									break;
									case '待付款':
										html += '<button type="button" class="mui-btn order_cancel" data-order_id="'+ data.datas.order_info.order_id +'">取消订单</button>'+
												'<button type="button" class="mui-btn RGB payment" data-order_id="'+ data.datas.order_info.order_id +'" data-goods_total="'+ data.datas.order_info.goods_amount +'" data-order_sn="'+ data.datas.order_info.order_sn +'">付款</button>';
									break;
									case '待发货':
										html += '<button type="button" class="mui-btn">配送中</button>';
									break;
									case '待收货':
										html += '<button type="button" class="mui-btn RGB">取消订单</button>'+
												'<button type="button" class="mui-btn RGB order_receive" data-order_id="'+ data.datas.order_info.order_id +'">确认收货</button>';
									break;
									case '交易完成': 
										if(evaluation_state == 1){
											html += '<button type="button" class="mui-btn">已评价</button>';
										}else{
											html += '<button type="button" class="mui-btn RGB evaluate" data-order_id="'+ data.datas.order_info.order_id +'">评价</button>';
										}
									break;
//									case '已评价':
//										html += '<button type="button" class="mui-btn">已评价</button>';
//									break;
								}
				}
				$.each(data.datas.order_info.goods_list, function(k,v) {
					list += '<ul class="mui-table-view mui-grid-view" ng-repeat="x in rs">'+
								'<li class="mui-table-view-cell mui-col-xs-3">'+
									'<img class="mui-media-object mui-pull-left" src="'+ v.image_url +'"/>'+
								'</li>'+
								'<li class="mui-table-view-cell mui-col-xs-9">'+
									'<div class="mui-ellipsis-2 title">'+ v.goods_name +'</div>'+										
									'<div class="money">'+
										'<span>￥'+ v.goods_price +'</span><span class="mui-pull-right num">x'+ v.goods_num +'</span>'+
									'</div>'+
								'</li>'+
								'<div class="details_1">'+
									'<li class="mui-table-view-cell">'+
										'<span>类型</span>'+
										'<span class="mui-pull-right ">'+ v.goods_spec +'</span>'+
									'</li>'+
								'</div>'+
							'</ul>';
				});		
				$('.state_desc').html(html);
				$('.order_goods').html(list);
			}else{
				toast(data.datas.error);
			}
			
			//取消订单
			$('.order_cancel').unbind('tap');
			$('.order_cancel').on('tap',function(){
				var _this = $(this);
				var order_id = _this.attr('data-order_id');
				order_cancel(member_id,order_id,'order.html');
			})
			//确认收货
			$('.order_receive').unbind('tap');
			$('.order_receive').on('tap',function(){
				var _this = $(this);
				var order_id = _this.attr('data-order_id');
				order_receive(member_id,order_id,'order.html');
			})
			//付款
			$('.payment').unbind('tap');
			$('.payment').on('tap',function(){
				var _this = $(this);
				var order_id = _this.attr('data-order_id');   //订单id
				var order_sn = _this.attr('data-order_sn');   //订单号
				var goods_total = _this.attr('data-goods_total');   //支付金额
				plus.nativeUI.actionSheet({cancel:"取消",buttons:[{title:"余额支付"},{title:"支付宝支付"},{title:"微信支付"}]},function(e){
					if(e.index == 1){
						$('.password-of-pay-wraper').css("display","block");
						$('#password_backdrop').css("display","block");
						$('.btn').slideUp()				
						
						//如果用户选择的是余额支付，则执行如下代码
						tool.shade(function (){
							$("#j_password_of_pay").hide();
						}).show();  
						//默认让第一个输入框获得焦点
						setTimeout(function (){
							$("#j_password_of_pay").show().find("input:password").eq(0).focus();
						},10); 
						var pwdInputs = $("#j_password_of_pay input:password");
						pwdInputs.each(function (index,item){
							$(this).on("keyup",function (){
								if(!tool.isEmpty($(this).val())){
									$(this).next().focus();
									//如果是最后一个输入框，则执行ajax操作
									if(index == pwdInputs.size() - 1){
										var pwd = "";
										$(this).blur();
										pwdInputs.each(function (){
											pwd += $(this).val();
										}); 
										yuezhifu(order_id,pwd);
		//									alert("用户输入的密码是："+ pwd +"，发送ajax，检查用户输入密码是否正确");	
		//									$("#j_password_of_pay").hide();
		//									tool.shade().hide();
		//									$('.popover').fadeOut();
									}
								}
							});
						});
					}else if(e.index == 2){
						ptype = 'alipay';
						payment(order_sn,goods_total);
					}else if(e.index == 3){
						ptype = 'wxpay';
						payment(order_sn,goods_total);
					}
				});
				
			})

			
			
			
			//评价 
			$('.evaluate').unbind('tap');
			$('.evaluate').on('tap',function(){
				var _this = $(this);
				var order_id = _this.attr('data-order_id');
				var nwaiting = plus.nativeUI.showWaiting();//显示原生等待框
				var webviewContent= plus.webview.create('./Evaluation.html','Evaluation.html',{},{order_id:order_id,spage:ws.id,zt:2});//后台创建webview并打开show.html
				webviewContent.addEventListener("loaded", function() { //注册新webview的载入完成事件
			        nwaiting.close(); //新webview的载入完毕后关闭等待框
			        webviewContent.show("slide-in-right",300); //把新webview窗体显示出来，显示动画效果为速度200毫秒的右侧移入动画
			    }, false);
			})
		},
		error:function(e){
			console.log(JSON.stringify(e));
			error();
		}
	});
},false)
	
